import time
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from common.Utils import BlogDriver
class BlogLogin:
    url=""
    driver=""
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_login.html"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)
    #正常登录
    def LoginRigText(self):
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #BlogDriver.getScreenshot()
        #如果出现昵称，证明登录成功
        time.sleep(1)
        txt=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3").text
        print(txt)
       # BlogDriver.getScreenshot()
       # self.driver.quit()

    #返回登录页面

       # self.driver.back()

    #异常登录
    def LoginFailText(self):
        #time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        #BlogDriver.getScreenshot()
        #time.sleep(2)
        #正确账号和错误密码
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(1)

        alter=self.driver.switch_to.alert
        #BlogDriver.getScreeshot()
        alter_text=alter.text
        print(alter_text)
        #time.sleep(3)
        assert alter_text == "密码错误"
        alter.accept()
        BlogDriver.getScreenshot()

#错误账号和正确密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("li")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        time.sleep(1)
        alert = self.driver.switch_to.alert
        assert alert.text == "用户不存在"
        alert.accept()
        BlogDriver.getScreenshot()

        # 错误的账号和错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("li")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()


        time.sleep(1)
        alert = self.driver.switch_to.alert
        assert alert.text == "用户不存在"
        alert.accept()
        BlogDriver.getScreenshot()

        # 空的账号和空的密码

        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        time.sleep(1)
        alert = self.driver.switch_to.alert
        assert alert.text == "账号或密码不能为空"
        alert.accept()
    BlogDriver.getScreenshot()



#Login=BlogLogin()
#Login.LoginRigText()
#Login.LoginFailText()